#include <windows.h>
#include <cstring>

//-----------------------------------------------------------------------

LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);				

//-----------------------------------------------------------------------

HWND hWnd;
HWND hwndLabel;
HWND hwndEdit;
HWND hwndButton;
char RES[10];
int Result;
int DaNumba = 57;

//---------------------------------------------------------------------

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
	LPSTR lpCmdLine, int nCmdShow) {

	WNDCLASS wc;

	wc.lpszClassName = "GuessNumber";
	wc.lpfnWndProc = MainWndProc;
	wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
	wc.lpszMenuName = "";
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	RegisterClass( &wc );

	hWnd = CreateWindow(
		"GuessNumber", "Guess Number",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		185,
		175,
		NULL,
		NULL,
		hInstance,
		NULL);

	hwndLabel = CreateWindow( 
		"Static", 
		"Guess a number between 1 and 100.", 
		WS_VISIBLE | WS_CHILD,
		10,
		10, 
		155, 
		50, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndEdit = CreateWindow( 
		"EDIT", 
		NULL, 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
		10,
		70, 
		155, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndButton = CreateWindow( 
		"BUTTON",
		"Guess", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
		10,
		100, 
		155, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	ShowWindow(hWnd, nCmdShow);

	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0 )) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}

//---------------------------------------------------------------------

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, 
	WPARAM wParam, LPARAM lParam) {
	HWND hwndCtl = (HWND) lParam;
	switch (msg) {
		case WM_COMMAND:
			switch (wParam) {
				case BN_CLICKED:
					if (hwndCtl == hwndButton) {
						GetWindowText(hwndEdit, RES, 10);
						Result = atoi(RES);
						if (Result == DaNumba)
						    SetWindowText(hwndLabel, 
                                                    "Correct guess!");
						else if (Result > DaNumba)
							SetWindowText(hwndLabel,
                                                     "You guessed too high!");
						else if (Result < DaNumba)
							SetWindowText(hwndLabel,
                                                     "You guessed too low!");
						else
							SetWindowText(hwndLabel,
                                                     "Not betwee 1 and 10 - try again!");
					}	
			}
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

//---------------------------------------------------------------------
